<?php

/**
 * Description of Promotion_relate
 *
 * @author Administrator
 */
class Promotion_relate extends CI_Controller {

  protected $class;
  protected $model;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    $model_name = $this->class . "_model";
    $this->model = new $model_name();
    set_userdata('currentURL', current_url());
  }

  public function create($promotion_id = '') {
    $promotion = $this->promotion_model->get($promotion_id);
    if ($promotion) {
      $promotions_by_provider = $this->promotion_model->get_all(
                      array('provider_user_id' => $promotion['provider_user_id'], 'id != ' => $promotion_id));
      if (!empty($promotions_by_provider)) {
        $temp_related = $this->model->get_all(array('promotion1_id' => $promotion_id));
        $promotion_related = array('promotion2_id' => array());
        foreach ($temp_related as $r) {
          $promotion_related['promotion2_id'][] = $r['promotion2_id'];
        }
        $this->load->view('admin/' . $this->class . '/create', array(
            'obj' => $promotion_related,
            'promotion_id' => $promotion_id,
            'relate_promotions' => $promotions_by_provider));
      } else {
        set_flash_message($this->lang->line('message.promotion.empty'));
        redirect('admin/promotion/show/' . $promotion_id . '/relate');
      }
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $promotion_id)));
      redirect('admin/promotion');
    }
  }

  public function save($promotion_id = '') {
    if (empty($promotion_id)) {
      $promotion_id = empty($_REQUEST['promotion1_id']) ? 0 : $_REQUEST['promotion1_id'];
    }
    if (!empty($promotion_id)) {
      $this->model->delete(array('promotion1_id' => $promotion_id));
      if (!empty($_REQUEST['promotion2_id'])) {
        $promotion2_id = is_array($_REQUEST['promotion2_id']) ? $_REQUEST['promotion2_id'] : array($_REQUEST['promotion2_id']);
        foreach ($promotion2_id as $promotion2) {
          $obj = array('promotion1_id' => $promotion_id, 'promotion2_id' => $promotion2);
          $this->model->save($obj);
        }
        set_flash_message($this->lang->line('message.promotion_relate.saved'));
        if (!empty($_REQUEST['btnAction']) && $_REQUEST['btnAction'] == 'media') {
          redirect('admin/promotion_media/create/' . $promotion_id);
        } else {
          redirect('admin/promotion/show/' . $promotion_id);
        }
        return;
      }
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('promotion'), $promotion_id)));
    }
    $promotion = $this->promotion_model->get($promotion_id);
    if ($promotion) {
      $promotions_by_provider = $this->promotion_model->get_all(
                      array('provider_user_id' => $promotion['provider_user_id'], 'id != ' => $promotion_id));
      $this->load->view('admin/' . $this->class . '/create', array(
          'obj' => $_REQUEST,
          'promotion_id' => $promotion_id,
          'relate_promotions' => $promotions_by_provider));
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $promotion_id)));
      redirect('admin/promotion');
    }
  }

}